package com.clgc.eduservice.controller;


import com.clgc.eduservice.entity.EduClass;
import com.clgc.eduservice.service.EduClassService;
import com.clgc.utils.R;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 班级表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-04-27
 */
@RestController
@RequestMapping("/eduservice/edu-class")
public class EduClassController {

    @Autowired
    EduClassService classService;


    @ApiOperation("新增班级")
    @PostMapping("addClass")
    public R addClass(@ApiParam("班级信息") @RequestBody EduClass eduClass) {

        boolean flag = classService.save(eduClass);
        return flag ? R.ok() : R.error().message("添加班级失败");
    }

    @ApiOperation("找到所有的班级信息")
    @GetMapping("getAllClass")
    public R getAllClass() {

        List<EduClass> list = classService.list(null);

        return R.ok().data("list", list);
    }

    @ApiOperation("按照id找到班级")
    @GetMapping("getClassById/{id}")
    public R getClassById(@ApiParam("班级id") @PathVariable String id) {
        EduClass aClass = classService.getById(id);
        return R.ok().data("class", aClass);
    }

    @ApiOperation("修改班级信息")
    @PostMapping("updateClass")
    public R updateClass(@ApiParam("修改后的班级信息") @RequestBody EduClass eduClass) {

        boolean flag = classService.updateById(eduClass);
        return flag ? R.ok() : R.error().message("修改班级信息失败");
    }

    @ApiOperation("按照id删除班级")
    @DeleteMapping("deleteClassById/{id}")
    public R deleteClass(@ApiParam("班级id") @PathVariable String id) {

        boolean flag = classService.removeById(id);
        return flag ? R.ok() : R.error().message("删除操作失败");
    }

}

